{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pymatbridge as pymat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge-051089d9-1f6e-4298-8228-e9e1a8b3f368\n",
      "Send 'exit' command to kill the server\n",
      ".......MATLAB started and connected!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<pymatbridge.pymatbridge.Matlab at 0x7f4a10638080>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matlab = pymat.Matlab()\n",
    "matlab.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "func_file = os.path.dirname(pymat.__file__) + '/matlab/www/demo_func.m'\n",
    "res = matlab.run_func(func_file, {'a': 10})\n",
    "res['result']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.,  4.,  8.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res_dict = matlab.run_code('a=[1,3,7]')\n",
    "res_dict = matlab.run_code('plot(a)')\n",
    "res_dict = matlab.run_code('b=a+1')\n",
    "matlab.get_variable('b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "b =\n",
       "\n",
       "     2     4     8\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAgAAAAGACAIAAABUQk3oAAAACXBIWXMAABcSAAAXEgFnn9JSAAAA\nB3RJTUUH3wgFAjkLssA/9QAAACR0RVh0U29mdHdhcmUATUFUTEFCLCBUaGUgTWF0aFdvcmtzLCBJ\nbmMuPFjdGAAAACJ0RVh0Q3JlYXRpb24gVGltZQAwNC1BdWctMjAxNSAyMjo1NzoxMWrauvUAAB7C\nSURBVHic7d1/UFT3/e/xj6gRwVJqTRV20NWkK/4WRDJh/EmIWsZfTdtv0lRMYIy/gExTyThqEyE3\nnanJ3DiKgzVOow6mMfUHI+3NTYISaidcxYBCKhZBwSYLKgk6JuGH6673j5NuCCIuy+5+Puec5+Ov\n9cg6755u3i9gz9lXvzt37ggAgPkEyR4AACAHAQAAJkUAAIBJEQAAYFIEAACYFAEAACZFAACASREA\nAGBSBAAAmBQBAAAmRQAAgEkRAABgUgQAAJgUAQAAJkUAAIBJEQAAYFIEAACYFAEAACZFAACASQ2Q\nPUD3Tp06dfr06StXrgQHB8fGxs6ZMyckJET2UABgKP1UK4VvaWlZs2bN2bNnOx8MCwt74403Zs6c\nKWsqADAe5X4FtG7durNnz06bNu3QoUP/+te/Tpw4sXr16ps3b2ZkZDQ2NsqeDgCMQ60AaGlpKS0t\nHTBgQF5e3qRJkwYOHDh8+PAXXnhhzpw57e3tRUVFsgcEAONQKwD+/e9/CyHCw8PDw8M7H4+LixNC\n1NfXyxkLAIxIrQCYPn36gAEDvv76a4fD0fn4hQsXxH9jAADgE2oFwMCBA9PT09vb29evX3/jxg0h\nhMvlOnz4cGFh4fjx4+fPny97QAAwDuWuAhJCFBQU/PnPf7506VJwcLDD4Rg0aNCvfvWrNWvWhIWF\nyR4NAIxDufsArl69WlhYWFtbGxkZabPZbty4cfbs2ffeey8+Pn7u3Ln3elZubm5ZWVkg5wSAXjk1\n8n9+M2vi5vmjZQ/yHbV+Amhra0tOTm5sbMz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ZkyxVTfo8FPeFG5OZ3O8vLytra2oKCg6OhoFT4F2o0AAACT\n4k1gADApAgAATIoAAACTIgAAwKQIAAAwKQIAAEyKAAAAkyIAAMCkCAAAMCkCAABMigAAAJMiAADA\npAgAADCp/w/UelOBKsEppQAAAABJRU5ErkJggg==\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "res_dict = matlab.run_code('figure; plot(b); b')\n",
    "\n",
    "from IPython.core.displaypub import publish_display_data\n",
    "imgfiles = res_dict['content']['figures']\n",
    "text_output = res_dict['content']['stdout']\n",
    "\n",
    "display_data = []\n",
    "if text_output:\n",
    "    display_data.append({'text/plain':text_output})\n",
    "for imgf in imgfiles:\n",
    "    try: # python 2\n",
    "        image = open(imgf, 'rb').read().decode('utf-8') \n",
    "    except: # python 3\n",
    "        image = open(imgf, 'rb').read()\n",
    "        \n",
    "    display_data.append({'image/png': image})\n",
    "\n",
    "for d in display_data:\n",
    "        publish_display_data(d)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "."
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matlab.is_connected()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MATLAB closed\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matlab.stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matlab.is_connected()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
